//
//  UITableViewCell+MKUI.m
//  MKUI
//
//  Created by sushangjin on 2018/9/10.
//  Copyright © 2018年 sushangjin. All rights reserved.
//

#import "UITableViewCell+MKUI.h"
#import "UIView+MKUI.h"
#import "UITableView+MKUI.h"

CGFloat const kMKUIAccessoryTypeDefaultLeftMargin = 10;
CGFloat const kMKUIAccessoryTypeDefaultRightMargin = 15;

@implementation UITableViewCell (MKUI)
//系统操作区域视图宽度
- (CGFloat)mk_accessorySystemTypeViewWidth{
    CGFloat width = 0;
    switch (self.accessoryType) {
        case UITableViewCellAccessoryNone:
            width = 0;
            break;
        case UITableViewCellAccessoryDisclosureIndicator:
            if(@available(iOS 13.0, *)){
                width = 11.5;
            }else{
                width = 8;
            }
            break;
        case UITableViewCellAccessoryDetailDisclosureButton:
            if(@available(iOS 13.0, *)){
                width = 44;
            }else{
                width = 42;
            }
            break;
        case UITableViewCellAccessoryCheckmark:
            if(@available(iOS 13.0, *)){
                width = 19.5;
            }else{
                width = 14;
            }
            break;
        case UITableViewCellAccessoryDetailButton:
            if(@available(iOS 13.0, *)){
                width = 25;
            }else{
                width = 22;
            }
            break;
        default:
            break;
    }
    return width;
}
//系统操作区域视图与contentView的左边距
- (CGFloat)mk_accessorySystemTypeViewLeftMargin{
    CGFloat margin = kMKUIAccessoryTypeDefaultLeftMargin;
    switch (self.accessoryType) {
        case UITableViewCellAccessoryNone:
            margin = 0;
            break;
        case UITableViewCellAccessoryDisclosureIndicator:
            if(@available(iOS 13.0, *)){
                margin = 0;
            }
            break;
        case UITableViewCellAccessoryDetailDisclosureButton:
            if(@available(iOS 13.0, *)){
                margin = -0.5;
            }
            break;
        case UITableViewCellAccessoryCheckmark:
            if(@available(iOS 13.0, *)){
                margin = 2.25;
            }
            break;
        case UITableViewCellAccessoryDetailButton:
            if(@available(iOS 13.0, *)){
                margin = -0.5;
            }
            break;
        default:
            break;
    }
    return margin;
}
//系统操作区域视图与UITableViewCell的右边距
- (CGFloat)mk_accessorySystemTypeViewRightMargin{
    CGFloat margin = kMKUIAccessoryTypeDefaultRightMargin;
    switch (self.accessoryType) {
        case UITableViewCellAccessoryNone:
            margin = 0;
            break;
        case UITableViewCellAccessoryDisclosureIndicator:
            if(@available(iOS 13.0, *)){
                margin = 20;
            }
            break;
        case UITableViewCellAccessoryDetailDisclosureButton:
            if(@available(iOS 13.0, *)){
                margin = 20;
            }
            break;
        case UITableViewCellAccessoryCheckmark:
            if(@available(iOS 13.0, *)){
                margin = 22.25;
            }
            break;
        case UITableViewCellAccessoryDetailButton:
            if(@available(iOS 13.0, *)){
                margin = 19.5;
            }
            break;
        default:
            break;
    }
    return margin;
}

- (CGFloat)mk_accessoryCustomViewLeftMargin{
    CGFloat width = kMKUIAccessoryTypeDefaultLeftMargin;
    if(@available(iOS 13.0, *)){
        width = 0;
    }
    return width;
}
- (CGFloat)mk_accessoryCustomViewRightMargin{
    CGFloat width = kMKUIAccessoryTypeDefaultRightMargin;
    if(@available(iOS 13.0, *)){
        width = 20;
    }
    return width;
}
- (CGFloat)mk_accessoryViewLeftMargin{
    CGFloat margin = 0;
    if(self.accessoryView==nil){
        margin = self.mk_accessorySystemTypeViewLeftMargin;
    }else{
        margin = self.mk_accessoryCustomViewLeftMargin;
    }
    return margin;
}
- (CGFloat)mk_accessoryViewRightMargin{
    CGFloat margin = 0;
    if(self.accessoryView==nil){
        margin = self.mk_accessorySystemTypeViewRightMargin;
    }else{
        margin = self.mk_accessoryCustomViewRightMargin;
    }
    return margin;
}
- (CGSize)mk_sizeThatFits:(CGSize)size sizeFitsBlock:(CGSize(^)(CGSize size))block{
    size.height = 99999999;
	UITableView *tableView = (UITableView *)[self mk_firstSuperViewWithClass:[UITableView class]];
    CGSize limitSize = self.contentView.bounds.size;
    limitSize.height = MAX(size.height,limitSize.height);
	CGSize s = CGSizeZero;
	if(block){
		s = block(limitSize);
	}
    if(s.height>0&&![UITableView mk_isAutoAddSeparatorHeightToCell]){
        s.height += tableView.mk_separatorHeight;
    }
    //消除浮点误差
    s.height = ceil(s.height);
	return s;
}
@end
